package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.ManagerMapper;
import com.shop.pojo.Manager;
import com.shop.service.ManagerService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;
    @Override
    public Manager findManagerName(String managername) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("manager_name",managername);
        return managerMapper.selectOne(queryWrapper);
    }

    @Override
    public Manager findUserName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        return managerMapper.selectOne(queryWrapper);
    }

    @Override
    public void add(Manager manager) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        String password=bCryptPasswordEncoder.encode(manager.getPassword());
        manager.setPassword(password);
        managerMapper.insert(manager);
        managerMapper.insertRoleAndManager(manager.getId(),manager.getRoleId());
    }

    @Override
    public Page<Manager> pageQuery(Integer pagenum, Integer pageSize, String pageQuery) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        Page<Manager> page = new Page<>(pagenum,pageSize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("manager_id",pageQuery).or().like("manager_name",pageQuery);
            page = managerMapper.selectPage(page,wrapper);
            List<Manager> managerList = page.getRecords();
            for (Manager manager:
                 managerList) {
                String roleName = managerMapper.findRoleNameByManagerId(manager.getId());
                manager.setRoleName(roleName);
            }
            page.setRecords(managerList);
        }else{
            page = managerMapper.selectPage(page,null);
            List<Manager> managerList = page.getRecords();
            for (Manager manager:
                    managerList) {
                String roleName = managerMapper.findRoleNameByManagerId(manager.getId());
                manager.setRoleName(roleName);
            }
            page.setRecords(managerList);
        }
        return page;
    }

    @Override
    public void deleteById(Integer id) {
        long count = managerMapper.findCountByManagerId(id);
        if (count>0){
            new RuntimeException();
        }
        managerMapper.deleteRoleAndManager(id);//先删中间表才能删原来表
        managerMapper.deleteById(id);
    }

    @Override
    public void edit(Manager manager) {
        managerMapper.updateById(manager);
        managerMapper.updateRoleAndManager(manager.getId(),manager.getRoleId());
    }

    @Override
    public Manager findById(Integer id) {
        Manager manager = managerMapper.selectById(id);
        Integer roleId = managerMapper.selectRoleIdByManagerId(id);
        manager.setRoleId(roleId);
        return manager;
    }

    @Override
    public List<Manager> findAll() {
        return managerMapper.selectList(null);
    }
}
